package com.android.lovingpet.common.postlist;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.bean.PostEvent;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PostListFragment extends BaseFragment<PostListContract.View, PostListPresenter> implements PostListContract.View {

    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private PostListAdapter adapter;
    private List<Post> posts;
    private int typeOrUserId;

    public static PostListFragment newInstance(int mode, int typeOrUserId) {
        Bundle args = new Bundle();
        args.putInt("mode", mode);
        args.putInt("typeOrUserId", typeOrUserId);
        PostListFragment fragment = new PostListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_post_list;
    }

    @Override
    protected void onCreateView(View view) {
        EventBus.getDefault().register(this);
        typeOrUserId = getArguments().getInt("typeOrUserId");
        recyclerView = view.findViewById(R.id.rv_moment);
        posts = new ArrayList<>();
        adapter = new PostListAdapter(posts);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        //recyclerView.setNestedScrollingEnabled(false);
        //recyclerView.setRefreshHeader(recyclerView.getDefaultRefreshHeaderView());
        recyclerView.setAdapter(adapter);
        /*recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getPresenter().getPosts(typeOrUserId, 0);
            }

            @Override
            public void onLoadMore() {
                getPresenter().getPosts(typeOrUserId, posts.size());
            }
        });*/
        refreshLayout = view.findViewById(R.id.refresh_layout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getPresenter().getPosts(typeOrUserId, 0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getPresenter().getPosts(typeOrUserId, posts.size());
            }
        });
        refresh();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public PostListPresenter createPresenter() {
        return new PostListPresenter(getArguments().getInt("mode"));
    }

    @Override
    public PostListContract.View createView() {
        return this;
    }

    @Override
    public void addPosts(List<Post> posts) {
        this.posts.addAll(posts);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearPosts() {
        posts.clear();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void setUser() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void refresh() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //recyclerView.refresh();
                refreshLayout.autoRefresh();
            }
        });
    }

    @Override
    public void complete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //recyclerView.refreshComplete();
                //recyclerView.loadMoreComplete();
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
            }
        });
    }

    @Subscribe
    public void onEvent(PostEvent event) {
        if (event.getType() == typeOrUserId) {
            refresh();
        }
    }
}
